package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.ResponseResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/myself")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册用户
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        //校验账号或者邮箱是否存在
        User existUser = userService.findByAccountOrEmail(user);
        if (existUser != null) {
            return ResponseResult.fail(500, "账号或者邮箱已存在");
        }
        userService.register(user);

        return ResponseResult.success();
    }
    @PostMapping("/updateByUserId")
    public ResponseResult updateByUserId(@RequestBody User user){
        System.out.println("user: " + user);
        userService.updateByUserId(user);
        return ResponseResult.success();
    }
    @PostMapping("/updateAvatar")
    public ResponseResult updateAvatar(@RequestParam("file")MultipartFile file,
                                       @RequestParam("userId")Integer userId) throws IOException {
        String url = userService.updateAvatar(file, userId);
        return ResponseResult.success(url);
    }
    @PostMapping("/updatePassword")
    public ResponseResult updatePassword(@RequestBody User user){
        System.out.println("user: " + user);
        userService.updatePassword(user);
        return ResponseResult.success();
    }
}
